using System;
using System.Collections.Generic;
using System.Text;

namespace cuberga.Workflow
{
    public class Process: AbstractPublisher
    {
        private AbstractLink _linkToNextProcess;
        private bool _isMarkerOn = false;
        protected ICoreProcess _coreProcess;

        public Process(ICoreProcess coreProcess)
        {
            _coreProcess = coreProcess;
        }

        public void Perform()
        {
            if (_coreProcess != null) 
                _coreProcess.Perform();

            SetState(_coreProcess);
            PropagateMarker();
        }

        public virtual void AddLink(AbstractLink newLink)
        {
            _linkToNextProcess = newLink;
        }

        public virtual void SetMarker()
        {
            _isMarkerOn = true;
        }

        public virtual bool IsReadyToPerform
        {
            get { return _isMarkerOn; }
        }

        private void PropagateMarker()
        {
            UnsetMarker();
            SendMarkerToFollowers();
        }
        
        protected virtual void UnsetMarker()
        {
            _isMarkerOn = false;
        }

        protected virtual void SendMarkerToFollowers()
        {
            if (!(_linkToNextProcess == null))
            {
                _linkToNextProcess.PropagateMarker();
            }
        }
    }
}
